package com.top.mall.main.home.adapter

import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.image.loadRoundImage
import com.top.mall.R
import com.top.mall.custom.GridSpacingItemDecoration
import com.top.mall.entity.Banner
import com.top.mall.entity.CateList
import com.top.mall.main.goods.GoodsCategoryActivity
import com.youth.banner.adapter.BannerAdapter


class HomeLabelAdapter :
    BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_home_label_child) {

    val itemViews: MutableMap<Int, TextView> = mutableMapOf()

    override fun convert(holder: BaseViewHolder, item: String) {
        itemViews[holder.layoutPosition] = holder.getView<TextView>(R.id.text_heads_label_name)
    }
}

class HomeBannerAdapter :
    BannerAdapter<Banner, com.top.mall.base.BaseViewHolder>(mutableListOf()) {
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): com.top.mall.base.BaseViewHolder {
        val imageView = ImageView(parent.context)
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.setPadding(
            ConvertUtils.dp2px(13f), 0,
            ConvertUtils.dp2px(13f), 0
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return com.top.mall.base.BaseViewHolder(imageView)
    }

    override fun onBindView(holder: com.top.mall.base.BaseViewHolder, data: Banner, position: Int, size: Int) {
        (holder.itemView as ImageView).loadRoundImage(data.image,radius = 15)
    }

}

class HomeCateGroupAdapter :
    BaseQuickAdapter<MutableList<CateList>, BaseViewHolder>(R.layout.item_home_cate_group) {

    override fun convert(holder: BaseViewHolder, item: MutableList<CateList>) {
        holder.getView<RecyclerView>(R.id.rvCateGroupPage).apply {
            val decoration = GridSpacingItemDecoration(
                spanCount = 5, spacing = ConvertUtils.dp2px((13f))
            )
            addItemDecoration(decoration)
        }.adapter = HomeCateChildAdapter().apply {
            setNewInstance(item)
        }
    }

}

class HomeCateChildAdapter :
    BaseQuickAdapter<CateList, BaseViewHolder>(R.layout.item_home_cate_child) {

    init {
        setOnItemClickListener { _, _, position ->
            val itemData = getItem(position)
            GoodsCategoryActivity.open(itemData)
        }
    }

    override fun convert(holder: BaseViewHolder, item: CateList) {
        holder.setText(R.id.text_category_name,item.name)
            .getView<ImageView>(R.id.image_category_icon)
            .loadCircleImage(item.image)
    }

}

class HomeCateCursorAdapter() :
    BaseQuickAdapter<Int, BaseViewHolder>(R.layout.item_home_cate_cursor) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: Int) {
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class HomeMultipleItem<T>(override val itemType: Int, val data: T) : MultiItemEntity

